package com.hzhk.happyrent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzhk.happyrent.R;
import com.hzhk.happyrent.ui.adapter.viewpager.GuideAdapter;
import com.hzhk.happyrent.utils.MyPerference;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WelcomeActivity extends AppCompatActivity {


    @BindView(R.id.welcome_guide)
    ViewPager welcomeGuide;
    @BindView(R.id.welcome_launcher)
    ImageView welcomeLauncher;
    @BindView(R.id.circIndicator)
    LinearLayout circIndicator;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    private int img1 = R.drawable.circleindecitor;
    private int img2 = R.drawable.circleindicator_bac;
    private List<ImageView> dotViewLists = new ArrayList<ImageView>();
    private int imgSize = 15;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme_Fullscreen);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        welcomeLauncher.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        if (MyPerference.getInstance().getFirstIn()) {
            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
            alphaAnimation.setStartOffset(1000);
            alphaAnimation.setDuration(500);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
//                    if (!MyPerference.getInstance().getFirstIn()) {
//                        startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
//                        WelcomeActivity.this.finish();
//                    }
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    welcomeLauncher.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            welcomeLauncher.startAnimation(alphaAnimation);
            initWelcome();
        } else {
            startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
            finish();
        }
    }

    private void initWelcome() {
        GuideAdapter guideAdapter = new GuideAdapter(this);
        for (int i = 0; i < 3; i++) {
            ImageView imageView = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            //为小圆点左右添加间距
            params.leftMargin = 10;
            params.rightMargin = 10;
            //手动给小圆点一个大小
            params.height = imgSize;
            params.width = imgSize;
            if (i == 0) {
                imageView.setBackgroundResource(img1);
            } else {
                imageView.setBackgroundResource(img2);
            }
            //为LinearLayout添加ImageView
            circIndicator.addView(imageView, params);
            dotViewLists.add(imageView);
        }
        final AtomicBoolean misScrolled = new AtomicBoolean(false);
        welcomeGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (dotViewLists.size() > 1) {
                    for (int i = 0; i < dotViewLists.size(); i++) {
                        //选中的页面改变小圆点为选中状态，反之为未选中
                        if (position == i) {
                            ((View) dotViewLists.get(i)).setBackgroundResource(img1);
                        } else {
                            ((View) dotViewLists.get(i)).setBackgroundResource(img2);
                        }
                    }
                }
                if (position == 2) {
                    tvEnter.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
//                switch (state) {
//                    case ViewPager.SCROLL_STATE_DRAGGING:
//                        misScrolled.set(false);
//                        break;
//                    case ViewPager.SCROLL_STATE_SETTLING:
//                        misScrolled.set(true);
//                        break;
//                    case ViewPager.SCROLL_STATE_IDLE:
//                        if (welcomeGuide.getCurrentItem() == welcomeGuide.getAdapter().getCount() - 1 && !misScrolled.get()) {
//                            MyPerference.getInstance().setIsFirst(false);
//                            startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
//                            WelcomeActivity.this.finish();
//                        }
//                        misScrolled.set(true);
//                        break;
//                }
            }
        });
        welcomeGuide.setAdapter(guideAdapter);
    }

    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        MyPerference.getInstance().setIsFirst(false);
        startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
        finish();
    }
}
